package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.LoginFailedException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Employee::getUsername, username);
        Employee employee = baseMapper.selectOne(queryWrapper);
        // 用户为空
        if (null == employee) {
            throw new AccountNotFoundException("查询不到账户异常");
        }
        // 账户禁用
        if (employee.getStatus() == StatusConstant.DISABLE) {
            throw new AccountLockedException("用户已经被禁用了");
        }
        password = digestPassword(password);
        // 密码不对
        if (!password.equals(employee.getPassword())) {
            throw new LoginFailedException("用户密码错误");
        }
        return employee;
    }

    private static String digestPassword(String password) {
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        return password;
    }


    @Override
    public void saveEmployee(EmployeeDTO employeeDTO) {
        Employee employee = Employee.builder()
                .name(employeeDTO.getName())
                .username(employeeDTO.getUsername())
                // 初始密码
                .password(digestPassword("123456"))
                .phone(employeeDTO.getPhone())
                .status(StatusConstant.ENABLE)
                .sex(employeeDTO.getSex())
                .idNumber(employeeDTO.getIdNumber())
                .build();
        baseMapper.insert(employee);
    }

    /**
     * 员工分页查询
     *
     * @param requestParam
     * @return
     */
    @Override
    public PageResult pageList(EmployeePageQueryDTO requestParam) {
        Page<Employee> page = new Page<>(requestParam.getPage(), requestParam.getPageSize());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotBlank(requestParam.getName()), Employee::getName, requestParam.getName())
                .eq(Employee::getStatus, StatusConstant.ENABLE);
        Page<Employee> employeePage = baseMapper.selectPage(page, queryWrapper);
        List<Employee> records = employeePage.getRecords();
        return new PageResult(employeePage.getTotal(), records);
    }

    @Override
    public Employee getEmplyeeById(long id) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Employee::getId, id)
                .eq(Employee::getStatus, StatusConstant.ENABLE);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(Employee::getId, employeeDTO.getId());
        baseMapper.update(employee, updateWrapper);
    }

    @Override
    public void startOrStop(Integer status, long id) {
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(Employee::getStatus, status)
                .eq(Employee::getId, id);
        baseMapper.update(updateWrapper);
    }


}

